//
// Maths.cpp for  in /home/goldbe_y//tek3/Projets/Maths/302graphe
// 
// Made by yoni goldberg
x// Login   <goldbe_y@epitech.net>
// 
// Started on  Mon Sep 24 14:39:22 2012 yoni goldberg
// Last update Fri Oct 12 18:05:53 2012 yoni goldberg
//

#include "Maths.hpp"

Maths::Maths(const std::string &str)
{
  Parse	tmp(str);

  _count = 0;
  _str = "";
  _save = "";
  _sommets = 0;
  _tab = tmp.get_from_file();
  _line = tmp.getLine();
  nombre_sommet();
  _adj = new std::string[_sommets];
  _result = new int[_sommets];
  osef = new int[_sommets];
  fill_adj();
}

Maths::~Maths()
{
}

int	Maths::get_line()
{
  return _line;
}

int	Maths::get_sommets()
{
  return _sommets;
}

std::vector<std::string>	Maths::get_tab()
{
  return _tab;
}

std::string			*Maths::get_adj()
{
  return _adj;
}

int				*Maths::get_res()
{
  return _result;
}

int				*Maths::get_osef()
{
  return osef;
}

void	Maths::aff_tab(std::vector<std::string> tab)
{
  for (int i = 0; i < tab.size(); i++)
    std::cout << tab[i] << std::endl;
}

void	Maths::nombre_sommet()
{
  for (int i = 0; i < _line; i++)
    {
      check(_tab[i][0]);
      check(_tab[i][2]);
    }
  _sommets = _str.size();
}

void	Maths::check(char c)
{
  int	i = 0;

  if (_str.size() == 0)
    _str += c;
  while (_str[i] && _str[i] != c){i++;};
  if (i >= _str.size())
      _str += c;
}

bool	Maths::check2(std::string str, char c)
{
  for (int i = 0; i < _str.size(); i++)
    {
      if (str[i] == c)
	return false;
    }
  return true;
}

void	Maths::fill_adj()
{
  for (int i = 0; i < _line; i++)
    { 
      if (check2(_adj[_tab[i][0] - 97], _tab[i][2]))
	_adj[_tab[i][0] - 97] += _tab[i][2];
      if (check2(_adj[_tab[i][2] - 97], _tab[i][0]))
	_adj[_tab[i][2] - 97] += _tab[i][0];
    }  
}

void	swap(int *a, int *b)
{
  int	tmp;

  tmp = *a;
  *a = *b;
  *b = tmp;
}

void	Maths::tri_selection(int *tab, int size)
{
  int i;
  int max;

  while(size > 0)
    {
      max = 0;
      for(i = 1; i < size; i++)
	if(tab[i] >= tab[max])
	  if(osef[i] > osef[max] || tab[i] != tab[max])
	      max = i;	
      swap(&tab[max], &tab[size - 1]);
      swap(&osef[max], &osef[size - 1]);
      size--;
    }
}

void				Maths::do_parcour()
{
  srand(time(NULL));
  int	som = (rand() % _sommets) + 97;
  int	count = 0;

  for (int i = 97; i < _sommets + 97; i++)
    osef[i - 97] = i;
  parcours_largeur(som, 0);
  tri_selection(_result, _sommets);
}

bool	Maths::check3(char c)
{
  int i = 0;

  if (_save.size() == 0)
    {
      _save += c;
      return true;
    }
  while (_save[i] && _save[i] != c){i++;};
  if (i >= _save.size())
    {
      _save += c;
      return true;
    }
  return false;
}

void	Maths::parcours_largeur(int som, int count)
{
  std::string tmp("");

  _result[som - 97] = count;

  count += 1;					
  check3(som);			
  for (int i = 0; i < _adj[som - 97].size(); i++)		
    if (check3(_adj[som - 97][i]))				
      tmp += _adj[som - 97][i];
  for (int i = 0; i < tmp.size(); i++)
    parcours_largeur(tmp[i], count);
}
